//
//  BrandViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "BrandViewController.h"
#import "BrandTableViewCell.h"
#import "TAMacro.h"
#import "ZWYNetTool.h"

#import "BrandFirst.h"
#import "BrandData.h"
#import "BrandItems.h"
#import "BranddetailViewController.h"

#import "MJRefresh.h"
#import "MBProgressHUD.h"




@interface BrandViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) BrandFirst *first;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray <BrandItems *> *dataArray;

@end

@implementation BrandViewController

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.094 alpha:1.000];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;
    self.dataArray = [NSMutableArray array];
    self.page = 1;
    [self createTableView];

    [self createData];
}

- (void)createData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");

    NSString *str1 = @"http://mobile.iliangcang.com/brand/brandList/?page=";
    NSString *str2 = @"&count=20&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSString *str = [str1 stringByAppendingString:[[NSString stringWithFormat:@"%ld", self.page] stringByAppendingString:str2]];
    
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if (self.page == 1) {
            self.dataArray = [NSMutableArray array];
        }
               self.first = [BrandFirst modelWithDic:result];
        for (BrandItems *item in self.first.data.items) {
            [self.dataArray addObject:item];
        }
        if (self.dataArray.count != 0) {
            [hud hideAnimated:YES afterDelay:0.5];
        }
        
        [self.tableView reloadData];
        self.page = self.page +1;
    } andFailureBlock:^(NSError *error) {
    }];
}


- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 99, WIDTH, HEIGHT - 143) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.137 alpha:1.000];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[BrandTableViewCell class] forCellReuseIdentifier:@"reuse"];
}
// 上拉加载
- (void)upLoad {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(createData)];
}

// 下拉刷新(段东松手后会执行的方法)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.tableView.contentOffset.y < 0) {
//        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
//        [hud hideAnimated:YES afterDelay:1];
        self.page = 1;
        [self createData];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self upLoad];
    BrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.data = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BranddetailViewController *detailVC = [[BranddetailViewController alloc] init];
    detailVC.dataID = self.dataArray[indexPath.row].brand_id;
    detailVC.name = self.dataArray[indexPath.row].brand_name;
    detailVC.logo = self.dataArray[indexPath.row].brand_logo;
    [self.navigationController pushViewController:detailVC animated:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
